package com.smartthinking.dive;

import java.io.File;
import java.util.Dictionary;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class FavoritosArrayAdapterAnunciantes extends BaseAdapter{
	  private final Context context;
	  private final List<Dictionary<String,String>> values;
	  Utilities util;
	  String ban_destino_url;
	  private static final String HTTPS = "https://";
		private static final String HTTP = "http://";
//	  private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	  public FavoritosArrayAdapterAnunciantes(Context context, List<Dictionary<String,String>> values) {
		    this.context = context;
		    this.values = values;
		    util = new Utilities();
	  }
	  
	  @Override
	  public View getView(int position, View convertView, ViewGroup parent) {
	    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	     final Dictionary<String,String> dic = values.get(position);
	    
	    
	    if(! (dic.get("Header") == null)){
	    	convertView = inflater.inflate(R.layout.banner_anunciantes, parent, false);
	        ImageView banner = (ImageView) convertView.findViewById(R.id.imageViewbanneranun);
	        File storagePath = Environment.getExternalStorageDirectory();
	        
//			Bitmap bmp = BitmapFactory.decodeFile(storagePath+"/Squenda/"+dic.get("Header").toString());
	        Bitmap bmp = BitmapFactory.decodeFile(context.getFilesDir()+"/localdata/"+dic.get("Header").toString());

			banner.setImageBitmap(bmp);
			banner.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					String tag2="";
					Log.i("BANNER DESTINO", ""+dic.get("Destino"));
					if(!dic.get("Destino").startsWith(HTTP) && !dic.get("Destino").startsWith(HTTPS)){
						tag2 =  HTTP + dic.get("Destino");
					}
					Uri uri = Uri.parse(tag2);
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(intent);
				}
			});
	        
	    }else{
	    	String nombre ="";
	    	convertView = inflater.inflate(R.layout.lista_dest_anunciantes, parent, false);
	        TextView texto = (TextView) convertView.findViewById(R.id.textViewdestanun);
	        RelativeLayout relative = (RelativeLayout) convertView.findViewById(R.id.RelativeLayoutdestanun);
	        nombre = dic.get("poi_nombre").toString();
	    	texto.setText(nombre);
	    	
	             
	    	 if(dic.get("poi_destacado").equals("1")){
	    	    	texto.setTextColor(Color.WHITE);
	    	    	relative.setBackgroundColor(Color.parseColor("#B89EEC"));
	    	    }else{
	    	    	texto.setTextColor(Color.BLACK);
	    	    	relative.setBackgroundColor(Color.WHITE);
	    	    }
	    }    	
	    
	   
	    
	    return convertView;
	  }

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return values.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return values.get(arg0).get("eve_nombre");
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	
//	@Override
//	public int getPositionForSection(int section) {
//		// If there is no item for current section, previous section will be selected
//		for (int i = section; i >= 0; i--) {
//			for (int j = 0; j < getCount(); j++) {
//				if (i == 0) {
//					// For numeric section
//					for (int k = 0; k <= 9; k++) {
//						if (StringMatcher.match(String.valueOf(((String) getItem(j)).charAt(0)), String.valueOf(k)))
//							return j;
//					}
//				} else {
//					if (StringMatcher.match(String.valueOf(((String) getItem(j)).charAt(0)), String.valueOf(mSections.charAt(i))))
//						return j;
//				}
//			}
//		}
//		return 0;
//	}
//
//	@Override
//	public int getSectionForPosition(int position) {
//		return 0;
//	}
//
//	@Override
//	public Object[] getSections() {
//		String[] sections = new String[mSections.length()];
//		for (int i = 0; i < mSections.length(); i++)
//			sections[i] = String.valueOf(mSections.charAt(i));
//		return sections;
//	}

	} 

